﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YJYpublicClass
{
    public partial class Frm_PLC通讯测试 : Form
    {
        public Frm_PLC通讯测试(PLC通讯接口 obj)
        {
            InitializeComponent();
            tempPLC = obj;
        }
        PLC通讯接口 tempPLC = null;

        private void Frm_PLC通讯测试_Load(object sender, EventArgs e)
        {
            this.cbx_连接状态.Checked = tempPLC.Connection;
            this.pdv_PLC参数.SelectedObject = (PLCconfig)tempPLC.cf;
        }

        private void btn_PLC连接_Click(object sender, EventArgs e)
        {
            tempPLC.Connect();
        }

        private void btn_断开连接_Click(object sender, EventArgs e)
        {
            tempPLC.Close();
        }

        private void btn_线圈写入_Click(object sender, EventArgs e)
        {
            Boolean value=true;
            if(string.Equals(this.txb_写入测试值.Text,"true",StringComparison.InvariantCultureIgnoreCase))
            {
                value=true;
            }
            else if(string.Equals(this.txb_写入测试值.Text,"1",StringComparison.InvariantCultureIgnoreCase))
            {
                value=true;
            }else
            {
                value=false;
            }
            if(tempPLC.WriteCoil(this.txb_写入测试地址.Text, value))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_Ushort写入_Click(object sender, EventArgs e)
        {
            if (tempPLC.WriteD(this.txb_写入测试地址.Text, this.txb_写入测试值.Text))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_float写入_Click(object sender, EventArgs e)
        {
            if (tempPLC.WriteF(this.txb_写入测试地址.Text, this.txb_写入测试值.Text))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_double写入_Click(object sender, EventArgs e)
        {

            if (tempPLC.WriteDouble(this.txb_写入测试地址.Text, this.txb_写入测试值.Text))
            {
                richTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                richTextBox1.AppendText("写入失败" + "\r\n");
            }
        }

        private void btn_线圈读取_Click(object sender, EventArgs e)
        {
            string value="";
            tempPLC.ReadCoil(this.txb_读取测试地址.Text, out value);
            richTextBox1.AppendText(value + "\r\n");
            this.txt_读取结果值.Text = value;
        }

        private void btn_ushort读取_Click(object sender, EventArgs e)
        {
            string value = "";
            tempPLC.ReadD(this.txb_读取测试地址.Text, out value);
            richTextBox1.AppendText(value + "\r\n");
            this.txt_读取结果值.Text = value;
        }

        private void btn_float读取_Click(object sender, EventArgs e)
        {
            string value = "";
            tempPLC.ReadF(this.txb_读取测试地址.Text, out value);
            richTextBox1.AppendText(value + "\r\n");
            this.txt_读取结果值.Text = value;
        }

        private void btn_double读取_Click(object sender, EventArgs e)
        {
            string value = "";
            tempPLC.ReadDouble(this.txb_读取测试地址.Text, out value);
            richTextBox1.AppendText(value + "\r\n");
            this.txt_读取结果值.Text = value;
        }

        private void btn_保存并更新_Click(object sender, EventArgs e)
        {
            if (XmlHelper.SaveXML(PublicValue.PLCconfigpath, 外设.mudbus.cf) == "")
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }

    }
}
